package action;

import java.util.HashMap;
import java.util.List;

import model.Role;
import model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import service.RoleService;
import action.base.BaseAction;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Controller
@Scope("prototype")
public class RoleAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private RoleService roleService;
	private PageList<HashMap<String, Object>> pageList;
	private List<HashMap<String, Object>> list;
	private int total;
	private Boolean success = false;
	private Integer roleFlag;
	private Role role;

	/**
	 * 角色分页列表
	 * 
	 * @return
	 */
	public String queryPage() {
		int page = super.getParameterInt("page");
		int limit = super.getParameterInt("limit");
		pageList = roleService.queryPage(page, limit);
		setTotal(pageList.getPaginator().getTotalCount());
		return "queryPage";
	}

	/**
	 * 新增 或 修改
	 * 
	 * @return
	 */
	public String saveOrUpdate() {
		roleService.saveOrUpdate(role);
		success = true;
		return "success";
	}

	/**
	 * 根据roleid拿到moduleid list
	 * 
	 * @param roleid
	 * @return
	 */
	public String queryModuleIds() {
		Integer roleid = super.getParameterInt("roleid");
		list = roleService.queryModuleByRole(roleid);
		return "list";
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		String ids = super.getParameterStr("ids");
		roleService.delete(ids);
		success = true;
		return "success";
	}

	/**
	 * 保存rolemodule
	 * 
	 * @param roleid
	 * @param moduleids
	 */
	public String saveRoleModule() {
		Integer roleid = super.getParameterInt("roleid");
		String[] moduleids = super.getParameterStrA("moduleids");
		roleService.saveRoleModule(roleid, moduleids);
		success = true;
		return "success";
	}

	public String queryUserRole() {
		User user = super.getUser();
		roleFlag = roleService.queryUserRole(user);
		setSuccess(true);
		return "roleFlag";
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public PageList<HashMap<String, Object>> getPageList() {
		return pageList;
	}

	public void setPageList(PageList<HashMap<String, Object>> pageList) {
		this.pageList = pageList;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<HashMap<String, Object>> getList() {
		return list;
	}

	public void setList(List<HashMap<String, Object>> list) {
		this.list = list;
	}

	public Integer getRoleFlag() {
		return roleFlag;
	}

	public void setRoleFlag(Integer roleFlag) {
		this.roleFlag = roleFlag;
	}

}
